//
//  SearchView.m
//  厚积薄发
//
//  Created by 焦钟培 on 15-1-8.
//  Copyright (c) 2015年 侯振兴. All rights reserved.
//

#import "SearchView.h"
#import "HistoryModel.h"
#import "HistoryBace.h"

@implementation SearchView
- (void)dealloc
{
    [_table release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatTable];
    }
    return self;
}

- (void)creatTable
{
    self.frame = [[UIScreen mainScreen] bounds];
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(20, 120, self.frame.size.width - 40, self.frame.size.height - 259) style:UITableViewStylePlain];

    _table.delegate = self;
    _table.dataSource = self;
    [self addSubview:_table];
    [_table release];
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = [HistoryBace selectAllHistory];
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    HistoryModel *model = [[HistoryBace selectAllHistory] objectAtIndex:indexPath.row];
    [cell.textLabel setText:model.history_tit];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HistoryModel *model = [[HistoryBace selectAllHistory] objectAtIndex:indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:model.history_tit forKey:@"tit"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SEARCH" object:self userInfo:dic];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_table setEditing:YES animated:YES];
    HistoryModel *model = [[HistoryBace selectAllHistory] objectAtIndex:indexPath.row];
    [HistoryBace deleteFromHistoryWithHistory_tit:model.history_tit];
    [_table setEditing:NO];
    [_table reloadData];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
